<?php

/**
 * Created by PhpStorm.
 * User: SnGef
 * Date: 2017/6/28
 * Time: 18:47
 */
class ElePassword
{
    //根据设备KEY，获取开锁密码
    public function GetElePassword($key)
    {
        $FIXED_SWAP_TEST[0] = array(3,6,5,4,8,9,0,1,7,2);
        $FIXED_SWAP_TEST[1] = array(7,2,9,8,3,1,6,5,4,0);
        $FIXED_SWAP_TEST[2] = array(9,7,0,2,8,5,4,6,3,1);
        $FIXED_SWAP_TEST[3] = array(5,2,1,4,0,3,6,7,8,9);
        $FIXED_SWAP_TEST[4] = array(5,6,1,9,3,8,0,2,7,4);
        $FIXED_SWAP_TEST[5] = array(8,1,2,4,0,9,6,3,7,5);
        $FIXED_SWAP_TEST[6] = array(6,7,1,4,0,3,5,2,8,9);
        $FIXED_SWAP_TEST[7] = array(1,3,6,7,0,4,8,5,9,2);
        $FIXED_SWAP_TEST[8] = array(4,8,0,5,3,2,1,6,9,7);
        $FIXED_SWAP_TEST[9] = array(6,5,3,7,2,0,1,9,4,8);

        $ORDER_SWAP_TEST[0] = array(1,2,0,4,3,		3,4,0,1,5,2,	4,3,1,3,1,0);
        $ORDER_SWAP_TEST[1] = array(4,2,1,0,3,		1,3,2,0,4,5,	4,1,4,2,2,3);
        $ORDER_SWAP_TEST[2] = array(2,1,0,4,3,		3,4,0,1,2,5,	0,4,0,2,3,4);
        $ORDER_SWAP_TEST[3] = array(2,0,1,4,3,		1,5,0,2,4,3,	3,4,1,4,3,3);
        $ORDER_SWAP_TEST[4] = array(4,0,2,1,3,		3,0,2,1,5,4,	2,1,4,0,2,2);
        $ORDER_SWAP_TEST[5] = array(2,1,4,3,0,		5,0,2,4,3,1,	2,1,3,2,0,1);
        $ORDER_SWAP_TEST[6] = array(3,4,2,0,1,		1,2,3,4,0,5,	1,1,3,3,0,0);
        $ORDER_SWAP_TEST[7] = array(4,3,1,0,2,		5,1,4,0,2,3,	1,3,4,3,1,3);
        $ORDER_SWAP_TEST[8] = array(1,4,0,3,2,		0,2,4,1,5,3,	0,0,0,3,4,3);
        $ORDER_SWAP_TEST[9] = array(3,0,1,2,4,		5,0,3,2,1,4,	0,0,0,3,4,3);

        $code_1 = (date("d") - 1) * 24 * 12
            + date("H") * 12
            + floor(date("i") / 5);
        $code_1 = (string)$code_1;
        $code_2 = ((date("Y") - 2015) * 12 + date("m")) % 7;
///////////////////////////////////////////////////////////////////////
//$code_1 = "5751";
///////////////////////////////////////////////////////////////////////
//$code_2 = 2;
///////////////////////////////////////////////////////////////////////
//3明码交换
        $code_3 = "";
        for($i = 0; $i < strlen($code_1); $i++){
            $ar_1 = $code_1[$i];
            $code_3 .= $FIXED_SWAP_TEST[$code_2][$ar_1];
        }
//4随机数
        $code_4 = $code_3 . rand(0, 9);
///////////////////////////////////////////////////////////////////////
//$code_4 = $code_3 . 8;
///////////////////////////////////////////////////////////////////////
//5求和得个位
        $sum = 0;
        for($i = 0; $i < strlen($code_4); $i++){
            $ar_4 = $code_4[$i];
            $sum += (int)$ar_4;
        }
        $code_5 = $code_4 . ($sum % 10);
//6明码交换
        $code_6 = "";
        for($i = 0; $i < strlen($code_5); $i++){
            $ar_5 = $code_5[$i];
            $j = $code_5[4];
            $code_6 .= $FIXED_SWAP_TEST[$j][$ar_5];
        }
        $code_6[4] = $code_5[4];
//7顺序调换
        $code_7 = "";
        $id = $code_6[5];
        $order = $ORDER_SWAP_TEST[$id];
        for($i = 0; $i < 6 ; $i++) {
            $j = $order[$i];
            $code_7 .= $code_6[$j];
        }
        $code_7[5] = $code_6[5];
//8顺序调换 明码末位
        $code_8 = "";
        $id = $code_1[3];
        $order = $ORDER_SWAP_TEST[$id];
        for($i = 5; $i < 11 ; $i++) {
            $j = $order[$i];
            $code_8 .= $code_7[$j];
        }
//9叠加
        $code_9 = "";
        $id = $code_1[3];
        $order = $ORDER_SWAP_TEST[$id];
        for($i = 0; $i < 6 ; $i++) {
            $j = $i + 11;
            $ar_9 = ($code_8[$i] + $order[$j]) % 10;
            $code_9 .= $ar_9;
        }

        return $code_9;
    }

}